<?php

/**
 * book actions.
 *
 * @package    Bookstore
 * @subpackage book
 * @author     Enkuso
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class bookActions extends sfActions
{
    public function executeList(sfWebRequest $request)
    {
        $this->books = array();
        $books = $this->getRoute()->getObjects();
        if($books && count($books)){
            foreach ($books as $book)
            {
                $this->books[$book->getId()] = $book->shortInfoAsArray();
            }
            $this->page = $request->getParameter('page', 1);
            $this->perpage = $request->getParameter('limit', sfConfig::get('app_max_per_page'));
            $this->total = BookTable::getBookCount();
            $this->pages = ceil($this->total / $this->perpage);
            $this->stat = 'ok';
        } else {
            $this->stat = 'fail';
            $this->error_code = 404;
            $this->error_msg = apiErrors::getErrorMessage($this->error_code);
        }

        switch ($request->getRequestFormat())
        {
            case 'yaml':
                $this->setLayout(false);
                $this->getResponse()->setContentType('text/yaml');
                break;
        }
    }

    public function executeSearch(sfWebRequest $request)
    {
        $this->books = array();
        $books = $this->getRoute()->getObjects();
        if($books && count($books)){
            foreach ($books as $book)
            {
                $this->books[$book->getId()] = $book->shortInfoAsArray();
            }
            $this->page = $request->getParameter('page', 1);
            $this->perpage = $request->getParameter('limit', sfConfig::get('app_max_per_page'));
            $this->total = BookTable::getInstance()->getSearchResultCount();
            $this->pages = ceil($this->total / $this->perpage);
            $this->search_term = $request->getParameter('query');
            $this->stat = 'ok';
        } else {
            $this->stat = 'fail';
            $this->error_code = 404;
            $this->error_msg = apiErrors::getErrorMessage($this->error_code);
        }
    }

    /**
     * Номын дэлгэрэнгүй мэдээлэл үзэх
     */
    public function executeInfo(sfWebRequest $request)
    {
        sfConfig::set('sf_escaping_strategy', false);

        $this->book = array();
        $book = $this->getRoute()->getObject();
        if($book){
            $this->book = $book->asArray($request->getHost());
            $this->stat = 'ok';
        } else {
            $this->stat = 'fail';
            $this->error_code = 1;
            $this->error_msg = apiErrors::getErrorMessage($this->error_code);
        }
    }

    /**
     * Бүтээлд бичсэн сэтгэгдлүүд авах
     */
    public function executeGetReviews(sfWebRequest $request)
    {
        $this->reviews = array();

        $page = $request->getParameter('page', 1);
        $limit = $request->getParameter('limit', sfConfig::get('app_max_per_page'));
        #$book_id = $request->hasParameter('id');
        $book_id = $this->getRoute()->getObject()->getId();
        $reviews = BookRateTable::getInstance()->getReviewsByBookId($book_id, $page,$limit);

        $user_id = $request->hasParameter('token') ? $this->getUser()->getGuardUser()->getId() : false;

        if(count($reviews)){
            foreach($reviews as $key => $review){
                $this->reviews[$key] = $review->bookReviewAsArray();
                $this->reviews[$key]['owner'] = $user_id == $review->getUserId() ? true : false;
            }
            $this->page = $page;
            $this->perpage = $limit;
            $this->total = BookRateTable::getBookReviewCount($book_id);
            $this->pages = ceil($this->total / $this->perpage);
            $this->stat = 'ok';
        } else {
            $this->stat = 'fail';
            $this->error_code = 7;
            $this->error_msg = apiErrors::getErrorMessage($this->error_code);
        }
    }
}
